﻿// <copyright file="BestSellerHistoryConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-03</date>
// <summary>Value Converter to convert a Book object to a Best Sellers History object</summary>

namespace NYTimes.ValueConverters
{
    using System;
    using System.Linq;
    using System.Windows.Data;
    using NYTimes;
    using NYTimes.Data;

    /// <summary>
    /// Value converter to convert a book to a best sellers history of that book
    /// </summary>
    public class BestsellerHistoryConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Convert a book to a BestSellerHistory for that book
        /// </summary>
        /// <param name="value">The source data being passed to the target </param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>a new BestSellerHistory object</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var history = new BestsellerHistory();

            var book = value as Book;

            if (book != null)
            {
                if (book.ISBNs == null)
                {
                    if (book.Details == null)
                    {
                        return null;
                    }

                    history.Title = book.Details.Title;
                }
                else
                {
                    history.ISBN = book.ISBNs.First().ISBN10;
                }
            }
            else
            {
                string stringValue = value as string;

                if (parameter == null)
                {
                    return null;
                }

                switch (parameter.ToString())
                {
                    case "title":
                        history.Title = stringValue;
                        break;

                    case "isbn":
                        history.ISBN = stringValue;
                        break;
                }
            }

            return history;
        }

        /// <summary>
        /// Not Implemented
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic. </param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the source object.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
